from flask import Flask, request
import redis
import time
import os
from Crypto.Hash import MD5
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5

application = Flask(__name__)

#读取openshift环境变量
priKey=os.environ['priKey01']+"\n"+os.environ['priKey02']+"\n"+os.environ['priKey03']+"\n"+os.environ['priKey04']+"\n"+os.environ['priKey05']+"\n"+os.environ['priKey06']+"\n"+os.environ['priKey07']+"\n"+os.environ['priKey08']+"\n"+os.environ['priKey09']
route01=os.environ['route01']
route02=os.environ['route02']
route01_A=os.environ['route01_A']
route01_B=os.environ['route01_B']
route01_C="	"+os.environ['route01_C']
route02_A=os.environ['route02_A']
route02_B=os.environ['route02_B']

rsakey = RSA.importKey(priKey)
signer = PKCS1_v1_5.new(rsakey)

def md5withrsa(data):
    h = MD5.new(data.encode("utf-8"))
    signature = signer.sign(h)
    return (
        "<!-- %s -->" %
        ''.join(
            '%.2x' %  
            x for x in signature) + "\n" + data)

@application.route("/")
def hello():
    return 'Shin'

@application.route(route01)
def obtainTicket():
    obtainTicket_data = route01_A + request.args.get('salt') + route01_B + request.args.get('userName') + route01_C
    return md5withrsa(obtainTicket_data)


@application.route(route02)									
def releaseTicket():
    releaseTicket_data = route02_A + request.args.get('salt') + route02_B
    return md5withrsa(releaseTicket_data)

if __name__ == "__main__":
    application.run()
